package cn.pomit.springbootwork.lock.redis.web;

import java.security.Principal;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.pomit.springbootwork.lock.redis.dto.ResultModel;
import cn.pomit.springbootwork.lock.redis.inter.ConsumeService;

@RestController
@RequestMapping("/lock")
public class LockRedisRest {
	private Logger log = LoggerFactory.getLogger(this.getClass());
	@Autowired
	ConsumeService consumeService;

	@RequestMapping(value = "/goodList")
	public ResultModel goodList(@RequestParam(value = "page", required = false) Integer page,
			@RequestParam(value = "size", required = false) Integer size) {
		return consumeService.goodList(page, size);
	}

	@RequestMapping(value = "/goodDetail")
	public ResultModel goodDetail(@RequestParam("goodId") Integer goodId) {
		return consumeService.goodDetail(goodId);
	}

	@RequestMapping(value = "/consume")
	public ResultModel consume(@RequestParam("goodId") Integer goodId, @RequestParam("num") Integer num,
			Principal principal) {
		String userName = "test";
		if (principal != null && !StringUtils.isEmpty(principal.getName())) {
			userName = principal.getName();
		}
		log.info("收到消费请求：userName：{};goodId:{}", userName, goodId);
		return consumeService.consume(goodId, num, userName);
	}
}
